#SQLInterface_Interface.py
# -*- coding:utf-8 -*-

import sqlite3 as sqlite

def opendb(dbname):
    connection = sqlite.connect(dbname)
    cursor = connection.cursor()
    return connection, cursor



def create(connection, cursor):
    """ This function create a database """
    try:
        cursor.execute('''CREATE TABLE items
                        (id INTEGER PRIMARY KEY,name VARCHAR(256),
                        bar_code VARCHAR(256),sn VARCHAR(256),
                        price INTEGER(16), count INTEGER(128))''')
    except sqlite.DatabaseError, x:
        print "Error: ", x
        connection.commit()


def add(connection, cursor, Iname, Ibar_code, Isn, Iprice, Icount):
    """ This function adds an item into a database """
    try:
        cursor.execute('INSERT INTO items VALUES(?,?,?,?,?,?)', 
                       (None,Iname, Ibar_code, Isn, Iprice, Icount))
    except sqlite.DatabaseError, x:
        print "Error: ", x


def printing(connection, cursor):
    try:
        cursor.execute('SELECT * FROM items')
        print cursor.fetchall()
    except sqlite.DatabaseError, x:
        print "Error", x
        connection.commit()


def find(connection, cursor, text):
    result = []
    try:
        cursor.execute('SELECT * FROM items')
        for row in cursor:
            if text in row[1]:
                result.append(row)
    
    except sqlite.DatabaseError, x:
        print "Error", x
    return result


def close(connection, cursor):
    connection.commit()
    connection.close()


#opendb("items.sqlite")
#create()

    
